package com.l.ssmRedis.controller;

import com.l.ssmRedis.pojo.User;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.HashOperations;
import org.springframework.data.redis.core.ListOperations;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.core.SetOperations;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurationSupport;

import java.util.*;

@Api(value = "redis测试", tags = "redis测试API")
@Controller
public class RedisController extends WebMvcConfigurationSupport {



    @Autowired
    private RedisTemplate redisTemplate;

    @ApiOperation(value = "传对象", notes = "测试传对象", httpMethod = "post", response = User.class)
    @ApiImplicitParams({
            @ApiImplicitParam(name = "id",value = "用户id",paramType = "query",dataType = "int",required = false),
            @ApiImplicitParam(name = "name",value = "用户名",paramType = "query",dataType = "String",required = true),
            @ApiImplicitParam(name = "password",value = "密码",paramType = "query",dataType = "String",required = true)
    })
    @RequestMapping(value = "/test_value",method = RequestMethod.POST)
    @ResponseBody
    public User test_value(User user) {
        System.out.println(user);
        redisTemplate.opsForValue().set("test_value", user);
        Object test_value = redisTemplate.opsForValue().get("test_value");
        return (User) test_value;
    }

    @RequestMapping(value = "/test_list",method = RequestMethod.POST)
    @ResponseBody
    public List test_list() {

        List<String> list = new ArrayList<>();
        list.add("aaa");
        list.add("bbb");
        ListOperations listOperations = redisTemplate.opsForList();
        listOperations.rightPushAll("test_list", list);
        ArrayList range = (ArrayList) listOperations.range("test_list", 0, -1);
        System.out.println(list);
        return range;


    }

    @RequestMapping(value = "/test_set",method = RequestMethod.POST)
    @ResponseBody
    public Set<String> test_set() {
        SetOperations<String, String> setOperations = redisTemplate.opsForSet();
        setOperations.add("test_set", "aaa");
        setOperations.add("test_set", "bbb");
        setOperations.add("test_set", "aaa");
        Set<String> test_set = setOperations.members("test_set");
        return test_set;
    }

    @RequestMapping(value = "/test_hash",method = RequestMethod.POST)
    @ResponseBody
    public ArrayList<String> test_hash() {
        HashOperations<String, String, String> hashOperations = redisTemplate.opsForHash();
        hashOperations.put("test_hash", "key1", "value1");
        hashOperations.put("test_hash", "key2", "value2");
        hashOperations.put("test_hash", "key1", "value1");
        hashOperations.put("test_hash1", "key1", "value3");
        String s = hashOperations.get("test_hash", "key1");
        String s1 = hashOperations.get("test_hash1", "key1");
        System.out.println(s);
        System.out.println(s1);
        ArrayList<String> list = new ArrayList<>();
        Collections.addAll(list, s, s1);
        return list;
    }


}
